package cn.org.donald.controller.game;


import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.dto.IdsDto;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.pojo.dto.game.TypeDto;
import cn.org.donald.pojo.entity.game.Type;
import cn.org.donald.service.game.TypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.org.donald.framework.base.BaseController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/game/type")
@Api(value = "游戏类型相关接口")
public class TypeController extends BaseController {


    @Reference(version = "1.0.0",group = "donald")
    private TypeService typeService;

    @PostMapping("/add")
    @ApiOperation(value = "游戏类型添加")
    @RequiresPermissions(value = "type:add:post")
    public HttpResult add(@RequestBody TypeDto typeDto){
        return typeService.add(typeDto) ? toResultSuccess(InfoConstant.ADD_SUCCESS) : toResultFail(InfoConstant.ADD_FAIL);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询类型列表")
    @RequiresPermissions("type:page:post")
    public TableResult listPage(@Validated @RequestBody PageDto<Type> pageDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        IPage<Type> page = typeService.findPage(pageDto);
        return toTableResult(page);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "修改类型")
    @RequiresPermissions("type:edit:put")
    public HttpResult edit(@RequestBody TypeDto typeDto){
        return typeService.editById(typeDto)?toResultSuccess(InfoConstant.EDIT_SUCCESS):toResultFail(InfoConstant.EDIT_FAIL);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除类型")
    @RequiresPermissions("type:delete:delete")
    public HttpResult delete(@Validated @RequestBody IdsDto ids, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        boolean flat = typeService.deleteById(ids.getIds());
        return flat?toResultSuccess(InfoConstant.DELETE_SUCCESS):toResultFail(InfoConstant.DELETE_FAIL,InfoConstant.ID_IS_NOT_EXIST);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "根据id查询类型详情")
    public HttpResult getOneById(@PathVariable Long id){
        Type type = typeService.findOneById(id);
        return toResultSuccess(type);
    }

}

